package Stack;

public class MyArrayStack {

    //初识化操作
    int[] arr;
    int size = 0;
    //传参构造方法
    public MyArrayStack(int size) {
        this.arr = new int[size];
    }

    //无传参构造方法
    public MyArrayStack() {
        arr = new int[10];
    }

    //判断是否满栈了
    private boolean isFull(){
        return size >= arr.length;
    }

    //判断是否空栈
    private boolean isEmpty(){
        return size == 0;
    }


    //入栈/压栈
    public void push(int data){
        if (isFull()){
            System.out.println("入栈操作失败，栈满");
            return;
        }
        arr[size] = data;
        size++;
        System.out.println("入栈成功");
    }

    //出栈/弹栈
    public int pop(){
        if (isEmpty()){
            System.out.println("出栈操作失败，栈为空");
            return -1;
        }

        int pollData = arr[--size];
        System.out.println("出栈操作成功");
        return pollData;
    }


    //查看栈顶元素
    public int peek(){
        if (isEmpty()){
            System.out.println("查看栈顶元素操作失败，栈为空");
            return -1;
        }

        return arr[size-1];
    }

    //遍历打印
    public void printAll(){
        for (int i = size-1; i >= 0; i--) {
            System.out.print(arr[i] + " ");
        }
    }

}
